/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_listener.c
 *
 * Purpose: implementation a listener to listen the dialer and create communication pipe
 *
 * Developer:
 *   wen.gu , 2022-03-12
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "cbus/bus/cbus_listener.h"

#include "cbus/core/cbus_log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define CHECK_OPS_API(ep_ops, ops_api) if(!(ep_ops)->ops_api){ \
    LOGE("syntax error: listener ops must implementation API:'%s'\n", #ops_api ); \
    return GFALSE; \
}
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/
struct _cbus_listener
{
    GPHD listener_handle;
    const cbus_pipe_ops* pipe_ops;
    cbus_listener_ops ops;
};
/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/
static GBOL cbus_listener_ops_check_valid(cbus_listener_ops* ops)
{
    CHECK_OPS_API(ops, initialize);
    CHECK_OPS_API(ops, finish);
    CHECK_OPS_API(ops, bind);
    CHECK_OPS_API(ops, unbind);
    CHECK_OPS_API(ops, accept);
    return GTRUE;
}
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

GErrc cbus_listener_create(cbus_listener_t* listener, const GSTR trans_secheme_name)
{
    if (!listener || !trans_secheme_name)
    {
        return G_ErrBadParameter;
    }

    cbus_transport_ops* ops = cbus_transport_find(trans_secheme_name);

    if (!ops)
    {
        return G_ErrNotFound;
    }

    if (cbus_listener_ops_check_valid(ops->listener) == GFALSE)
    {
        return G_ErrUndefined;
    }

    cbus_listener_t cl = (cbus_listener_t)malloc(sizeof(struct _cbus_listener));

    if (!cl)
    {
        return G_ErrInsufficientResources;
    }
    memset(cl, 0, sizeof(struct _cbus_listener));

    GErrc ret = ops->listener->initialize(&cl->listener_handle);

    if (G_OK == ret)
    {
        cl->pipe_ops = ops->pipe;
        cl->ops = *ops->listener;
        *listener = cl;
        return G_OK;
    }

    free(cl);

    return ret;
}

void cbus_listener_destroy(cbus_listener_t listener)
{
    if (listener)
    {
        GErrc ret = listener->ops.finish(listener->listener_handle);

        if (G_OK != ret)
        {
            LOGE("finish listener ops failed(0x%08x)\n", ret);
        }
        free(listener);        
    }
}

GErrc cbus_listener_bind(cbus_listener_t listener, const GSTR url)
{
    if (!listener || !url)
    {
        return G_ErrBadParameter;
    }

    return listener->ops.bind(listener->listener_handle, url);
}

GErrc cbus_listener_unbind(cbus_listener_t listener)
{
    if (!listener)
    {
        return G_ErrBadParameter;
    }

    return listener->ops.unbind(listener->listener_handle);
}

GErrc cbus_listener_accept(cbus_listener_t listener, cbus_pipe_t* com_pipe, GU32 timeout_ms)
{
    if (!listener || !com_pipe)
    {
        return G_ErrBadParameter;
    }

    GPHD pipe_handle = NULL;
    GErrc ret = listener->ops.accept(listener->listener_handle, &pipe_handle, timeout_ms);

    if (G_OK == ret)
    {
        return cbus_pipe_create(com_pipe, pipe_handle, listener->pipe_ops);
    }

    return ret;
}
